package com.arpa.wms.service.impl;

import cn.hutool.core.util.IdUtil;
import com.arpa.ntocc.common.common.cache.PartyCache;
import com.arpa.ntocc.common.common.util.CommonUtil;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.wms.domain.dto.InventoryOperationDto;
import com.arpa.wms.domain.dto.OutboundItemDTO;
import com.arpa.wms.domain.entity.OutboundItem;
import com.arpa.wms.domain.vo.OutboundItemVO;
import com.arpa.wms.domain.vo.OutboundItemWithRegisterVO;
import com.arpa.wms.mapper.OutboundItemMapper;
import com.arpa.wms.service.IOutboundItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 出库单明细 服务实现类
 * </p>
 *
 * @author LEO
 * @since 2020-08-07
 */
@Service
public class OutboundItemServiceImpl extends ServiceImpl<OutboundItemMapper, OutboundItem> implements IOutboundItemService {

    @Autowired
    private PartyCache partyCache;

    /**
     * 保存
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor={Exception.class})
    public boolean save(OutboundItem entity) {
        if(StringUtils.isBlank(entity.getCode())){
            entity.setCode(IdUtil.simpleUUID());
        }
        entity.setGroupCode(UserUtil.getBranchCode());
        if(StringUtils.isBlank(entity.getCreatedBy())){
            entity.setCreatedBy(UserUtil.getCode());
            entity.setCreatedName(partyCache.translate(UserUtil.getCode()));
        }
        return super.save(entity);
    }

    /**
     * 根据code更新实体
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor={Exception.class})
    public int updateByCode(OutboundItem entity) {
        if(StringUtils.isBlank(entity.getModifiedBy())){
            entity.setModifiedBy(UserUtil.getCode());
        }
        return baseMapper.update(entity,new QueryWrapper<OutboundItem>().lambda().eq(OutboundItem::getCode,entity.getCode()).eq(OutboundItem::getGroupCode,UserUtil.getBranchCode()));
    }

    /**
     * 查询列表
     * @param outboundItemDTO
     * @return
     */
    @Override
    public List<OutboundItemVO> queryList(OutboundItemDTO outboundItemDTO){
        outboundItemDTO.setGroupCode(UserUtil.getBranchCode());
        //排序字段名需要驼峰转数据库下划线类型字段名
        if(StringUtils.isNotEmpty(outboundItemDTO.getSortField())){
            outboundItemDTO.setSortField(CommonUtil.camel2Underline(outboundItemDTO.getSortField()));
        }
        List<OutboundItemVO> outboundItemVOList = baseMapper.queryList(outboundItemDTO);
        return outboundItemVOList;
    }

    /**
     * 查询合计,包含总数
     * @param outboundItemDTO
     * @return
     */
    @Override
    public OutboundItemVO queryListSum(OutboundItemDTO outboundItemDTO){
        outboundItemDTO.setGroupCode(UserUtil.getBranchCode());
        OutboundItemVO outboundItemVO = baseMapper.queryListSum(outboundItemDTO);
        return outboundItemVO;
    }

    /**
     * 查询所有
     * @param outboundItemDTO
     * @return
     */
    @Override
    public List<OutboundItemVO> queryAll(OutboundItemDTO outboundItemDTO) {
        outboundItemDTO.setGroupCode(UserUtil.getBranchCode());
        return baseMapper.queryAll(outboundItemDTO);
    }

    /**
     * @param outboundItemDTO :
     * @description 关联确认单查询结果集
     * @author xuyang
     * @date 2020/10/27 14:50
     **/
    @Override
    public List<OutboundItemWithRegisterVO> queryItemWithRegisterList(OutboundItemDTO outboundItemDTO) {
        outboundItemDTO.setGroupCode(UserUtil.getBranchCode());
        return this.baseMapper.queryItemWithRegisterList(outboundItemDTO);
    }

    /**
     * @param itemCode :
     * @param status   :
     * @description 根据拣货量与复核量+退减量的比较，尝试更新明细的状态
     * @author xuyang
     * @date 2020/10/29 8:55
     **/
    @Override
    public int updateStatusIfFinishRecheck(String itemCode, String status) {
        return this.baseMapper.updateStatusIfFinishRecheck(itemCode, status,UserUtil.getBranchCode());
    }

    /**
     * @param itemCode :
     * @param stockoutQuantity :
     * @description 尝试更新明细中的缺货数量，如果计划数量 < 已复核数量+已维护缺货数量+新缺货数量，则不更新
     * @author xuyang
     * @date 2020/10/29 10:24
     **/
    @Override
    public int updateStockout(String itemCode, BigDecimal stockoutQuantity) {
        return this.baseMapper.updateStockout(itemCode, stockoutQuantity);
    }

    /**
     * 根据分配单出库数量更新出库明细的出库数量
     *
     * @param outboundCode
     * @param wavePickingCode
     * @return
     */
    @Override
    public int updateOutQuantityByDistribution(String outboundCode, String wavePickingCode) {
        return this.baseMapper.updateOutQuantityByDistribution(outboundCode, wavePickingCode,UserUtil.getBranchCode());
    }

    /**
     * 根据出库登记表的复核数量更新出库明细复核数量和状态
     * @author rfwang
     * @date 2021/5/21 12:13
     * @param outboundCode 出库单号
     */
    @Override
    public int updateRecheckQuantity(String outboundCode,String groupCode) {
        return this.baseMapper.updateRecheckQuantity(outboundCode,groupCode);
    }


    /**
     * 订单取消更新出库明细数量
     *
     * @param wavePickingCode
     * @param allQuantityList
     * @author liuxr
     * @date 2021/5/21 21:23
     */
    @Override
    public void deliveryCancelUpdataQuantity(String wavePickingCode, List<InventoryOperationDto> allQuantityList) {
        this.baseMapper.deliveryCancelUpdataQuantity(wavePickingCode,allQuantityList,UserUtil.getBranchCode());
    }

    /**
     * 根据波次号和状态查询列表
     * @param outboundItemDTO
     * @return
     */
    @Override
    public List<OutboundItemVO> queryListGroupByWavePickingCodeAndStatus(OutboundItemDTO outboundItemDTO){
        outboundItemDTO.setGroupCode(UserUtil.getBranchCode());
        return this.baseMapper.queryListGroupByWavePickingCodeAndStatus(outboundItemDTO);
    }

    /**
     * 根据出库登记表的复核数量更新出库明细复核数量和状态
     * @author rfwang
     * @date 2021年7月16日
     * @param wavePickingCode 波次号
     */
    @Override
    public int updateRecheckQuantityByWavePickingCode(String wavePickingCode){
        return this.baseMapper.updateRecheckQuantityByWavePickingCode(wavePickingCode,UserUtil.getBranchCode());
    }


}
